package initPackage;

import java.io.File;
import java.io.FileOutputStream;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.KeyStore;
import java.security.SecureRandom;
import java.security.cert.Certificate;

import registration.RegistrationResources;
import server.ServerResources;
import shamir.shamirGenerator;
import certificate.X509CertificateHandler;

import com.google.common.io.Files;

public class InitAllCertificates
{
    static String keyStoreFile;
    static String DNName;
    static String hashKeyAlias;
    static String trustKeyStoreFile;
    static String shamirPubFile;
    static String shamirPrivFile;
    static String clientShamirPublicFile;
    static private char[] trustKeyStorePassword;
    static private char[] keyStorePassword;
    // serwerowe
    static
    {
        ServerResources resources = new ServerResources();
        RegistrationResources regResources = new RegistrationResources();
        keyStoreFile = (String)resources.getObject( "keyStoreFile" );
        keyStorePassword = (char[])resources.getObject( "keyStorePassword" );
        DNName = (String)resources.getObject( "PKWDNString" );
        hashKeyAlias = (String)resources.getObject( "hashKeyAlias" );
        trustKeyStorePassword = (char[])resources.getObject( "trustKeyStorePassword" );
        trustKeyStoreFile = (String)resources.getObject( "trustKeyStoreFile" );
        shamirPrivFile = (String)resources.getObject( "shamirPrivFile" );
        shamirPubFile = (String)resources.getObject( "shamirPubFile" );
        clientShamirPublicFile = (String)regResources.getObject( "PKWShamirPublicKeyFile" );
    }
    // klienta
    static String clientTrustedStoreFile = "src/registration/config/Trusted";

    public static void main( String[] args )
    {
        /**
         * tutaj musimy stworzyc od nowa pokolei:
         * 
         * 1. nowy server/config/PKWCert 2. nowy registration/config/Trusted
         * (zawierajacy powyzszy PKWCert) 3. nowy klucz publiczny i prywatny
         * hash_key
         */
        try
        {
            KeyStore pkw = KeyStore.getInstance( KeyStore.getDefaultType() );
            pkw.load( null, keyStorePassword );
            // nowy PKWCert
            KeyPairGenerator keyGen = KeyPairGenerator.getInstance( "RSA" );
            SecureRandom random = SecureRandom.getInstance( "SHA1PRNG" );
            keyGen.initialize( 2048, random );
            KeyPair pair = keyGen.generateKeyPair();
            Certificate cert = (Certificate)X509CertificateHandler.generate( DNName, pair, "SHA1withRSA", 30 );
            Certificate[] chain = { cert };
            pkw.setKeyEntry( "PKW", pair.getPrivate(), keyStorePassword, chain );
            // nowy hash_key
            // keyGen = KeyPairGenerator.getInstance("RSA");
            // random = SecureRandom.getInstance("SHA1PRNG");
            // keyGen.initialize(2048, random);
            // pair = keyGen.generateKeyPair();
            // cert = (Certificate) X509CertificateHandler.generate(DNName,
            // pair, "SHA1withRSA", 30);
            // Certificate[] hashChain = {cert};
            //
            // pkw.setKeyEntry(hashKeyAlias,pair.getPrivate(),keyStorePassword,hashChain);
            // nowy trusted klienta
            KeyStore clientTrustedKeyStore = KeyStore.getInstance( KeyStore.getDefaultType() );
            clientTrustedKeyStore.load( null, "".toCharArray() );
            KeyStore.Entry entry = new KeyStore.TrustedCertificateEntry( cert );
            clientTrustedKeyStore.setEntry( "PKW", entry, null );
            // clientTrustedKeyStore.setCertificateEntry("PKW", cert);
            // nowy trusted serwera
            // KeyStore serverTrustedKeyStore =
            // KeyStore.getInstance(KeyStore.getDefaultType());
            // serverTrustedKeyStore.load(null, "".toCharArray());
            // serverTrustedKeyStore.setCertificateEntry("PKW", cert);
            // teraz wszystko zapisujemy
            // client trsuted
            FileOutputStream o = new FileOutputStream( new File( clientTrustedStoreFile ) );
            clientTrustedKeyStore.store( o, "".toCharArray() );
            o.close();
            // PKWCert i hash
            o = new FileOutputStream( new File( keyStoreFile ) );
            pkw.store( o, keyStorePassword );
            o.close();
            // server trusted
            // o = new FileOutputStream(new File(trustKeyStoreFile));
            // serverTrustedKeyStore.store(o,trustKeyStorePassword);
            // o.close();
            // tworzymy shamira
            shamirGenerator sG = new shamirGenerator( shamirPrivFile, shamirPubFile );
            sG.generate();
            Files.copy( new File( shamirPubFile ), new File( clientShamirPublicFile ) );
            System.out.println( "Wszystko sie udalo" );
        }
        catch( Exception e )
        {
            e.printStackTrace();
        }
    }
}
